package com.minhua.jijiehao2.mainframe.circle.productTypeActivity.impl

import android.app.Activity
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseActivity
import com.minhua.jijiehao2.base.adapter.ViewHolder
import com.minhua.jijiehao2.base.adapter.recyclerview.BaseCommonAdapter
import com.minhua.jijiehao2.bean.ProductTypeBean
import com.minhua.jijiehao2.mainframe.circle.productTypeActivity.imp.IProductTypeView
import kotlinx.android.synthetic.main.activity_product_type.*

class ProductTypeActivity : BaseActivity<IProductTypeView, ProductTypePresenter>(), IProductTypeView {


    override fun getLayoutId(): Int = R.layout.activity_product_type

    override fun getPresenter(): ProductTypePresenter = ProductTypePresenter()

    private val listData = arrayListOf<ProductTypeBean>()
    private var adapter: BaseCommonAdapter<ProductTypeBean>? = null

    override fun initView() {
        refresh.setOnRefreshListener { initData() }
        adapter = object : BaseCommonAdapter<ProductTypeBean>(this, R.layout.item_product_type, listData) {
            override fun convert(holder: ViewHolder?, t: ProductTypeBean?) {
                holder?.setText(R.id.tvName, t?.itemValue)
                holder?.itemView?.setOnClickListener {
                    val intent = Intent()
                    intent.putExtra("name", t?.itemValue)
                    intent.putExtra("code", t?.itemCode)
                    setResult(Activity.RESULT_OK, intent)
                    finish()
                }
            }
        }
        rvContent.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        rvContent.adapter = adapter
    }

    override fun initData() {
        mPresenter.getProductTypes()
    }

    override fun onTypeSuccess(list: List<ProductTypeBean>) {
        refresh.isRefreshing = false
        listData.clear()
        listData.addAll(list)
        adapter?.notifyDataSetChanged()
    }
}
